﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.Threading;
using System.Data;
using EntityFramework.Extensions;
namespace Webcar.Controllers
{
    
    public partial class ContractController : BaseControler
    {
        public ActionResult List()
        {
            return View();
        }

        public ActionResult ListPartialView()
        {
            return PartialView();
        }
        public ActionResult Delete(int id)
        {
            using (var DB = new WebcarContext())
            {
                var model = DB.contract_info.Find(id);
                if (model != null)
                {
                    DB.contract_info.Remove(model);
                    DB.SaveChanges();
                }
            }
            return Success("List", "Contract");
        }
        [HttpGet]
        public ActionResult Edit(int? id)
        {
            contract_info model = null;
            if (id!=null)
            {
                model = WebcarDataProvider.DB.contract_info.Find(id);
            }
            if (model == null)
            {
                model = new contract_info();
            }
            return View(model: model);
        }
        [HttpPost]
        public ActionResult Edit(contract_info model)
        {
            if (model.dept_name.IsNull() || model.dept_name.IsNull())
            {
                ViewBag.ErrorMessage = "请输入单位。";
                return View(model);
            }
            if (model.man_name.IsNull() || model.man_name.IsNull())
            {
                ViewBag.ErrorMessage = "请输入姓名。";
                return View(model);
            }


            model.data_time = DateTime.Now;
            model.oper_code = CurrentUser.LoginUserID;
            using (var DB = new WebcarContext())
            {
                if (model.id > 0)
                {
                    var item = DB.contract_info.Find(model.id);
                    model.CopyPropertyTo(item);
                }
                else
                {
                    DB.contract_info.Add(model);
                }
                DB.SaveChanges();
            }
            return Success("List", "Contract");
        }
    }
}